Permission Denied: Connect, Error while sending  email using SMTP client submission java program
Hi Im trying to create java program, which send email using my outlook account with my company email address.

Below is program and error im facing. please help in resolve the issue.

package com.cigniti.utilities;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtility {
public static Session session;
public static String fromEmail="prathap.xxxx@xxxx.com";

static{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");

/*props.put("mail.smtp.socketFactory.fallback", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.ssl.enable", true);
*/


session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail,"xxxxxxx");//change accordingly
}
});
}

public String sendMessage(String toAddress,String ccAddress,String subject,String content){
String result="failed";
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(toAddress));
message.addRecipient(Message.RecipientType.CC,new InternetAddress(ccAddress));
message.setSubject(subject);
message.setText(content);

//send message
Transport.send(message);
result="sent";


} catch (MessagingException e) {
throw new RuntimeException(e);
}
return result;

}

public static void main(String[] args) {
new EmailUtility().sendMessage(fromEmail, fromEmail, "Java:Test:1", "Some messsage content");
}
}

Error Stack Trace:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.cigniti.utilities.EmailUtility.sendMessage(EmailUtility.java:54)
at com.cigniti.utilities.EmailUtility.main(EmailUtility.java:61)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at com.cigniti.utilities.EmailUtility.sendMessage(EmailUtility.java:49)
... 1 more

June 4th, 2015 2:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics